package com.erp.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.shop.service.IErpSellerStorePromotionActivityService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.gather.api.domain.dto.AutoQueryPriceTemplateInfoDto;
import com.ruoyi.shop.api.domain.ErpSellerStorePromotionActivity;
import com.ruoyi.shop.api.domain.bo.ErpSellerStorePromotionActivityBo;
import com.ruoyi.shop.api.domain.vo.ErpSellerStorePromotionActivityVo;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 商家促销活动
 *
 * @author erp
 * @date 2023-06-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStorePromotionActivity")
public class ErpSellerStorePromotionActivityController extends BaseController {

    private final IErpSellerStorePromotionActivityService iErpSellerStorePromotionActivityService;



    /**
     * 查询选择商家可用活动
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:storeActivityList")
    @GetMapping("/storeActivityList")
    public R<Object> storeActivityList(Long storeId) {
        return R.ok(iErpSellerStorePromotionActivityService.storeActivityList(storeId));
    }

    /**
     * 查询商家促销活动列表
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStorePromotionActivityVo> list(ErpSellerStorePromotionActivityBo bo, PageQuery pageQuery) {
        return iErpSellerStorePromotionActivityService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取商家促销活动详细信息
     *
     * @param activityId 主键
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:query")
    @GetMapping("/{activityId}")
    public R<ErpSellerStorePromotionActivityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long activityId) {
        return R.ok(iErpSellerStorePromotionActivityService.queryById(activityId));
    }

    /**
     * 新增商家促销活动
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:add")
    @Log(title = "商家促销活动", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStorePromotionActivityBo bo) {
        return toAjax(iErpSellerStorePromotionActivityService.insertByBo(bo));
    }

    /**
     * 修改商家促销活动
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:edit")
    @Log(title = "商家促销活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStorePromotionActivityBo bo) {
        return toAjax(iErpSellerStorePromotionActivityService.updateByBo(bo));
    }

    /**
     * 删除商家促销活动
     *
     * @param activityIds 主键串
     */
    @SaCheckPermission("seller:sellerStorePromotionActivity:remove")
    @Log(title = "商家促销活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] activityIds) {
        return toAjax(iErpSellerStorePromotionActivityService.deleteWithValidByIds(Arrays.asList(activityIds), true));
    }

    @PostMapping("/getPromotionActivityInfo")
    public R<Object> getPromotionActivityInfo(@RequestBody ErpSellerStorePromotionActivity activity) {
        return iErpSellerStorePromotionActivityService.getPromotionActivityInfo(activity);
    }


    /**
     * 将商品添加入店铺
     */
    @GetMapping("/addProductToActivity/{productId}/{activityId}/{discount}")
    public R<Object> addProductToActivity(
            @PathVariable("productId") Long productId,
            @PathVariable("activityId") Long activityId,
            @PathVariable("discount") BigDecimal discount
            ) {
        return iErpSellerStorePromotionActivityService.addProductToActivity(productId, activityId, discount);
    }

    @PostMapping("/processingProjectGroupAndDiscount")
    public R<Object> processingProjectGroupAndDiscount(@RequestBody AutoQueryPriceTemplateInfoDto templateInfo) {
        return iErpSellerStorePromotionActivityService.processingProjectGroupAndDiscount(templateInfo);
    }

    @PostMapping("/insertOrUpdate")
    R<Object>  insertOrUpdate(@RequestBody ErpSellerStorePromotionActivity activity){
        return iErpSellerStorePromotionActivityService.insertOrUpdate(activity);
    }

    @PostMapping("/selectActivityInfo")
    R<Object> selectActivityInfo(@RequestBody ErpSellerStorePromotionActivity activity){
        return iErpSellerStorePromotionActivityService.selectActivityInfo(activity);
    }


}
